package com.erhuogo.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.bean.Response;
import com.erhuogo.entity.OneCategory;
import com.erhuogo.enumerate.Result;
import com.erhuogo.service.OneCategoryService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 一级分类Action
 * @author aibinxiao
 * @date 2016年10月26日 上午11:33:40
 */
public class OneCategoryAction extends ActionSupport {
	private static final long serialVersionUID = 5680802495105859670L;
	private static Logger logger = LoggerFactory.getLogger(OneCategoryAction.class);
	
	// 模型驱动使用的对象
	private OneCategory oneCategory;

	public OneCategory getOneCategory() {
		return oneCategory;
	}

	public void setOneCategory(OneCategory oneCategory) {
		this.oneCategory = oneCategory;
	}
	
	// 注入OneCategoryService
	private OneCategoryService oneCategoryService;

	public OneCategoryService getOneCategoryService() {
		return oneCategoryService;
	}

	public void setOneCategoryService(OneCategoryService oneCategoryService) {
		this.oneCategoryService = oneCategoryService;
	}
	
	// 返回页面数据
	private Response response = new Response();

	public Response getResponse() {
		return response;
	}

	public void setResponse(Response response) {
		this.response = response;
	}

	/**
	 * 1.获取所有的一级商品分类信息
	 * @return
	 */
	public String getAllOneCategory(){
		List<OneCategory> listOneCategory = oneCategoryService.findAllOneCategory();
		if (listOneCategory != null && listOneCategory.size()>0){
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("获取一级商品分类列表成功");
			response.setData(listOneCategory);
		} else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("获取一级商品分类列表失败/列表为空");
			response.setData(listOneCategory);
		}
		return "allOneCategory";
	}
	
	/**
	 * 2.分页条件查询一级分类
	 * @return
	 */
	public String listPage(){
		String page = ServletActionContext.getRequest().getParameter("page");
		String rows = ServletActionContext.getRequest().getParameter("rows");
		String id = ServletActionContext.getRequest().getParameter("id");
		String name = ServletActionContext.getRequest().getParameter("name");
		String code = ServletActionContext.getRequest().getParameter("code");
		String startDate = ServletActionContext.getRequest().getParameter("startDate");
		String endDate = ServletActionContext.getRequest().getParameter("endDate");
		GridPageBean<OneCategory> listPage = oneCategoryService.findByPage(page, rows, id, name, code ,startDate, endDate);
		if(listPage!=null && listPage.getRecords()>0){
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("查询成功");
		}else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("查询失败");
		}
		response.setData(listPage);
		return "listPageSUCCESS";
	}
	
	/**
	 * 3.根据id查询一级分类
	 * @return
	 */
	public String findById(){
		String id = ServletActionContext.getRequest().getParameter("id");
		OneCategory oneCategory = oneCategoryService.findById(id);
		if(oneCategory!=null){
			response.setData(oneCategory);
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("查询成功");
		}else{
			response.setResult(Result.FAILED.toString());
			response.setMessage("查询失败");
		}
		return "findByIdSUCCESS";
	}
	
	/**
	 * 4.保存或者更新三级分类信息
	 * @return
	 */
	public String saveOneCategory(){
		String id = ServletActionContext.getRequest().getParameter("id");
		String code = ServletActionContext.getRequest().getParameter("code");
		String name = ServletActionContext.getRequest().getParameter("name");
		String desc = ServletActionContext.getRequest().getParameter("desc");
		try {
			oneCategoryService.save(id,code,name,desc);
			response.setResult(Result.SUCCESS.toString());
		} catch (Exception e) {
			response.setResult(Result.FAILED.toString());
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return "saveSUCCESS";
	}
	
	/**
	 * 5.开启或者屏蔽三级分类信息
	 * @return
	 */
	public String updateStatusById(){
		String id = ServletActionContext.getRequest().getParameter("id");
		String status = ServletActionContext.getRequest().getParameter("status");
		try {
			oneCategoryService.updateStatus(id,status);
			response.setResult(Result.SUCCESS.toString());
			response.setMessage("更新状态成功");
		} catch (Exception e) {
			response.setResult(Result.FAILED.toString());
			response.setMessage("更新状态失败");
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return "updateStatusSUCCESS";
	}
	
}
